<?php

class Default_Model_DbTable_Advertisement extends Zend_Db_Table_Abstract
{

    protected $_name = 'advertisement';
    
    //insert new advertisement
    public function InsertAdv($UserID,$AdName,$Adimg,$Position,$CompName,$Desc,$Status){
    	$AdvData = array("USER_ID"=>$UserID,"AD_NAME"=>$AdName,"AD_IMG"=>$Adimg,"POSITION"=>$Position
    			,"COMPANY_NAME"=>$CompName,"DESCRIPTION"=>$Desc,"STATUS"=>$Status);
    	$this->insert($AdvData);
    }
    //update Advertisement
    public function UpdateAdv($ID,$UserID,$AdName,$Adimg,$Position,$CompName,$Desc,$Status){
    	$AdvData = array("USER_ID"=>$UserID,"AD_NAME"=>$AdName,"AD_IMG"=>$Adimg,"POSITION"=>$Position
    			,"COMPANY_NAME"=>$CompName,"DESCRIPTION"=>$Desc,"STATUS"=>$Status);
    	$this->update($AdvData,"ID = $ID");
    }
    //Delete Advertisement
    public function DelAdv($ID){
    	return $this->delete("ID = $ID");
    }
    //Get all
    public function GetAllAdv(){
    	$select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
						->setIntegrityCheck(false);
    	$select
    	->from("advertisement AS adv")
    	->join("user AS u",
    			"u.ID = adv.USER_ID",array("u.USERNAME"))->group("adv.ID");
    	return $this->fetchAll($select);
    }
    //get adv by id
    public function GetById($Id){
    	return $Adv = $this->fetchRow("ID = $Id");
    }
    //get by name
    public function GetByName($Name){
    	return $this->fetchRow("AD_NAME = $Name");
    }
    //update status
    public function UpdateStt($Id){
    	$Stt = $this->fetchRow("ID = $Id");
    	if($Stt["STATUS"]==1){
    		$this->update(array("STATUS"=>"0"), "ID = $Id");
    	}
    	else{
    		$this->update(array("STATUS"=>"1"), "ID = $Id");
    	}
    }
    //update position
    public function UpdatePosition($Id){
    	$Position = $this->fetchRow("ID = $Id");
    	if($Position["POSITION"]==1){
    		$this->update(array("POSITION"=>"0"), "ID = $Id");
    	}
    	else{
    		$this->update(array("POSITION"=>"1"), "ID = $Id");
    	}
    }
}

